<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="../../css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/main.css" />

    <link rel="stylesheet" type="text/css" href="../../css/colleague.css" />

    <script type="text/javascript" src="../../script/api.js"></script>
    <script type="text/javascript" src="../../script/jquery.js"></script>

    <!-- <script type="text/javascript">
    apiready=function(){
    }
    </script> -->
    <script type="text/javascript" src="../../script/main.js"></script>
    <script type="text/javascript" src="../../script/bootstrap.js"></script>
</head>
<body>
  <div id="header">
      <div id="back"></div>
      <h3>编辑简历</h3>
  </div>
  <div class="span_input">
    <div class="nickname span_input_model">
      <span>昵称(选)：</span>
      <span><input type="text" placeholder = "" name="nickname" class="form-control" id = "nickname"></span>
    </div>
    <div class="realName span_input_model">
      <span>真实名字：</span>
      <span><input type="text" placeholder = "" name="realName" class="form-control" id = "realName"></span>
    </div>
    <div class="sex span_input_model">
      <span>性别：</span>
      <div class="span_input_model">
      <!-- <input name="cardgrade" type="radio" value="1"   checked="checked"/>男
      <input name="cardgrade" type="radio" value="2"  />女 -->
      		<select><option class="sex" value='0'>女</option><option class="sex" value='1'>男</option></select>
      </div>
    </div>
    <div class="birthday span_input_model">
      <span>生日：</span>
      <span><input type="text" placeholder = "" name="birthday" class="form-control" id = "birthday" readonly="readonly"></span>
    </div>
    <div class="studentId span_input_model">
      <span>学号(选)：</span>
      <span><input type="text" placeholder = "" name="studentId" class="form-control" id = "studentId"></span>
    </div>
    <div class="idCard span_input_model">
      <span>身份证号：</span>
      <span><input type="text" placeholder = "" name="idCard" class="form-control" id = "idCard"></span>
    </div>
    <div class="change_address span_input_model">
      <span>地址(选)：</span>
      <span><input type="text" placeholder = "" name="address" class="form-control" id = "address"></span>
    </div>
    <div class="function span_input_model">
      <span>求职意向(选)：</span>
      <span><input type="text" placeholder = "" name="function" class="form-control" id = "function"></span>
    </div>
    <div class="jobStatus span_input_model">
      <span>求职状态(选)：</span>
      <span><input type="text" placeholder = "" name="jobStatus" class="form-control" id = "jobStatus"></span>
    </div>
    <div class="workPlace span_input_model">
      <span>就业区域(选)：</span>
      <span><input type="text" placeholder = "" name="workPlace" class="form-control" id = "workPlace"></span>
    </div>
    <div class="">
        <span>* (选)标明的为选填项，为了信息的完整，建议全部填写！</span>
    </div>
  </div>
  <footer>
  <button class="myBtn" style="font-size:16px;">提交</button>
  </footer>
</body>
<script>
var age,date;
apiready=function(){
  back();
  var aid=$api.getStorage('aid');
  var url='/api/paTechnicaluser/'+aid;
  var functionRet=function(ret){
    console.log(ret.birthday);
    if(ret.birthday!=""){
      date=ret.birthday;
      age=ret.age;
    }else{
      date="1970-01-01";
      var now = new Date();
      age=now.getFullYear()-1970;
    }
    $("#nickname").val(ret.nickname);
    $("#realName").val(ret.realName);
    if(ret.sex==0){
      $("select").val(0);
    }else{
      $("select").val(1);
    }
    $("#birthday").val(date);
    $("#studentId").val(ret.studentId);
    $("#idCard").val(ret.idCard);
    $("#address").val(ret.address);
    $("#function").val(ret.function);
    $("#jobStatus").val(ret.jobStatus);
    $("#workPlace").val(ret.workPlace);
  }
  var functionErr=function(err){
    // date="1970-01-01";
    // $("#birthday").val(date);
    toast("加载原有简历资料失败！","bottom");
  }
  get(url,functionRet,functionErr);
}
$("#birthday").click(function(){
  showCalendar();
});
$(".myBtn").click(function(){
  var nickname = $("#nickname").val();
  if(nickname==0||nickname.length>6){
    // $("#nickname").addClass("border_warning").siblings().removeClass("border_warning");
    $("#nickname").addClass("border_warning");
    $("#nickname").parent().parent().siblings().find("input").removeClass("border_warning");
    // $("#nickname").attr("style","border: 1px solid red;");
    toast("昵称长度1-6个数字字母或汉字，请重新填写","bottom");
    return false;
  }
  var realName = $("#realName").val();
  if(RegeMatch()){
    $("#realName").addClass("border_warning");
    $("#realName").parent().parent().siblings().find("input").removeClass("border_warning");
    toast("真实姓名只能输入6位以内汉字，不含字母数字和特殊字符，请重新填写","bottom");
    return false;
  }
  var sex = $("select").val();
  var birthday = $("#birthday").val();
  var studentId = $("#studentId").val();
  var idCard = $("#idCard").val();
  var address = $("#address").val();
  if(address.length>20){
    $("#address").addClass("border_warning");
    $("#address").parent().parent().siblings().find("input").removeClass("border_warning");
    toast("地址长度请控制在20字以内，请重新填写","bottom");
    return false;
  }
  var _function = $("#function").val();
  if(_function.length>20){
    $("#function").addClass("border_warning");
    $("#function").parent().parent().siblings().find("input").removeClass("border_warning");
    toast("求职意向长度请控制在20字以内，请重新填写","bottom");
    return false;
  }
  var jobStatus = $("#jobStatus").val();
  if(jobStatus.length>20){
    $("#jobStatus").addClass("border_warning");
    $("#jobStatus").parent().parent().siblings().find("input").removeClass("border_warning");
    toast("求职状态长度请控制在20字以内，请重新填写","bottom");
    return false;
  }
  var workPlace = $("#workPlace").val();
  if(workPlace.length>20){
    $("#workPlace").addClass("border_warning");
    $("#workPlace").parent().parent().siblings().find("input").removeClass("border_warning");
    toast("就业区域长度请控制在20字以内，请重新填写","bottom");
    return false;
  }
  if(realName!=0&&realName.length<7){
    $("#realName").removeAttr("style");
    if(age>=0){
      $("#birthday").removeAttr("style");
      if(IdCardValidate(idCard)&&(idCard!=0)){
        $("#idCard").removeAttr("style");
        var jsons={
            "nickname":nickname,"realName":realName,"sex":sex,"age":age,"birthday":birthday,"studentId":studentId,"idCard":idCard,"address":address,
            "function":_function,"jobStatus":jobStatus,"workPlace":workPlace
        };
        var aid=$api.getStorage('aid');
        var url='/api/paTechnicaluser/'+aid;
        var functionRet=function(ret){
        }
        var functionErr=function(err){
          if (err.statusCode==200) {
              api.execScript({
                name: 'resume',
                script: 'location.href = "resume.html";toast("编辑资料成功！","bottom");'
              });
              api.execScript({
                name: 'root',
                frameName:'frame3',
                script: 'window.location.reload();toast("成功！","bottom");'
              });
              api.closeWin({
                  name: 'change'
              });
          }
          else {
            console.log(err.statusCode);
              toast("编辑资料失败！","bottom");
              // location.href = "change.html";
          }
        }
        put(url,jsons,functionRet,functionErr);
      }else{
        $("#idCard").addClass("border_warning");
        $("#idCard").parent().parent().siblings().find("input").removeClass("border_warning");
        // $("#realName").removeAttr("style");
        // $("#birthday").removeAttr("style");
        // $("#idCard").attr("style","border: 1px solid red;");
        toast("再检查一下身份证吧","bottom");
        // location.href = "change.html";
      }
    }else{
      $("#birthday").addClass("border_warning");
      $("#birthday").parent().parent().siblings().find("input").removeClass("border_warning");
      // $("#idCard").removeAttr("style");
      // $("#realName").removeAttr("style");
      // $("#birthday").attr("style","border: 1px solid red;");
      toast("再检查一下生日吧","bottom");
      // location.href = "change.html";
    }
  }else{
    $("#realName").addClass("border_warning");
    $("#realName").parent().parent().siblings().find("input").removeClass("border_warning");
    // $("#birthday").removeAttr("style");
    // $("#idCard").removeAttr("style");
    // $("#realName").attr("style","border: 1px solid red;");
      toast("真实姓名应不少于1个字，不大于6个字","bottom");
  }

})
function showCalendar(){
  api.openPicker({
    type: 'date',
    date : date,
    title: '选择时间'
  }, function(ret, err) {
    var now = new Date();
    var nowYear = now.getFullYear();
    var nowMonth = now.getMonth();
    var nowDay = now.getDay();
      if (ret) {
        var month,day;
        if(ret.month<10){
          month='0'+ret.month;
        }else{
          month=ret.month;
        }
        if(ret.day<10){
          day='0'+ret.day;
        }else{
          day=ret.day;
        }
        var now = new Date();
        var nowYear = now.getFullYear();
          $("#birthday").val( ret.year + '-' + month + '-' + day);
          age=nowYear-ret.year;
      } else {
          alert(JSON.stringify(err));
      }
  });
}
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];    // 加权因子
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];            // 身份证验证位值.10代表X
function IdCardValidate(idCard) {
    idCard = trim(idCard.replace(/ /g, ""));               //去掉字符串头尾空格
    if (idCard.length == 15) {
        return isValidityBrithBy15IdCard(idCard);       //进行15位身份证的验证
    } else if (idCard.length == 18) {
        var a_idCard = idCard.split("");                // 得到身份证数组
        if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){   //进行18位身份证的基本验证和第18位的验证
            return true;
        }else {
            return false;
        }
    } else {
        return false;
    }
}
/**
 * 判断身份证号码为18位时最后的验证位是否正确
 * @param a_idCard 身份证号码数组
 * @return
 */
function isTrueValidateCodeBy18IdCard(a_idCard) {
    var sum = 0;                             // 声明加权求和变量
    if (a_idCard[17].toLowerCase() == 'x') {
        a_idCard[17] = 10;                    // 将最后位为x的验证码替换为10方便后续操作
    }
    for ( var i = 0; i < 17; i++) {
        sum += Wi[i] * a_idCard[i];            // 加权求和
    }
    valCodePosition = sum % 11;                // 得到验证码所位置
    if (a_idCard[17] == ValideCode[valCodePosition]) {
        return true;
    } else {
        return false;
    }
}
/**
  * 验证18位数身份证号码中的生日是否是有效生日
  * @param idCard 18位书身份证字符串
  * @return
  */
function isValidityBrithBy18IdCard(idCard18){
    var year =  idCard18.substring(6,10);
    var month = idCard18.substring(10,12);
    var day = idCard18.substring(12,14);
    var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
    // 这里用getFullYear()获取年份，避免千年虫问题
    if(temp_date.getFullYear()!=parseFloat(year)
          ||temp_date.getMonth()!=parseFloat(month)-1
          ||temp_date.getDate()!=parseFloat(day)){
            return false;
    }else{
        return true;
    }
}
  /**
   * 验证15位数身份证号码中的生日是否是有效生日
   * @param idCard15 15位书身份证字符串
   * @return
   */
  function isValidityBrithBy15IdCard(idCard15){
      var year =  idCard15.substring(6,8);
      var month = idCard15.substring(8,10);
      var day = idCard15.substring(10,12);
      var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
      // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
      if(temp_date.getYear()!=parseFloat(year)
              ||temp_date.getMonth()!=parseFloat(month)-1
              ||temp_date.getDate()!=parseFloat(day)){
                return false;
        }else{
            return true;
        }
  }
//去掉字符串头尾空格
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
//真实姓名特殊符号限制
function RegeMatch(){
    var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
    if($("#realName").val() != "" && $("#realName").val() != null){
        if(!pattern.test($("#realName").val())){
            return true;
        }
    }
}
</script>
</html>
